<?php

class User_RegisterController extends Zend_Controller_Action{
	
	protected $_requestUri = null;
	
	public function init(){
		if($this->getRequest()){
			$this->_requestUri = $this->_getParam('nextPage'); 
		}
	}
	
	public function indexAction(){
		
		$form = new Custom_Form_CreateUser(array(
			'action'	=>	'/user/register',
			'method'	=>	'post'
		));
		
		$form->addElement('hidden', 'nextPage', array(
				'decorators'	=>	$form->hiddenDecorators,
				'value'	=>	$this->_requestUri
		));
		
		$request = $this->getRequest();
		if($request->isPost()){
			if($form->isValid($request->getPost())){
				Doctrine_Model_Users::createUser($form->getValues());
				
				$user = new Doctrine_Model_Users();
				$user->setPassword($form->password->getValue());
				
				$adapter = new Auth_Adapter($form->username->getValue(),$user->password);
				$result = Zend_Auth::getInstance()->authenticate($adapter);
				if($result){
					if($this->_getParam('nextPage'))
						$this->_helper->redirector->gotoUrl($this->_getParam('nextPage'));
					else{
//						display login successful
					}
				}else{
//					login unsuccessful
				}
			}
		}
		
		$this->view->form = $form;
	}
}